// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <QtGui/QImage>
#include <QtGui/QPainter>
#include <QtGui/QColor>

#include "ui/wizard.h"
#include "ui/introscreen.h"
#include "ui/clientconfigscreen.h"
#include "ui/configfixscreen.h"
#include "ui/progressscreen.h"
#include "ui/thanksscreen.h"
#include "util/provider.h"


namespace popdiag {

Wizard::Wizard(QWidget *parent): QWizard(parent) {
  SetupWizard();
  SetupPages();
  show();
}

Wizard::~Wizard() {
}

void Wizard::SetupPages() {
  IntroScreen *intro_screen = new IntroScreen(this);
  addPage(intro_screen);
  intro_screen->SetupUI();

  ClientConfigScreen *config_screen = new ClientConfigScreen(this);
  addPage(config_screen);
  config_screen->SetupUI();

  ConfigFixScreen *config_fix_screen = new ConfigFixScreen(this);
  addPage(config_fix_screen);
  config_fix_screen->SetupUI();

  ProgressScreen *progress_screen = new ProgressScreen(this);
  addPage(progress_screen);
  progress_screen->SetupUI();

  ThanksScreen *thanks_screen = new ThanksScreen(this);
  addPage(thanks_screen);
  thanks_screen->SetupUI();

  CHECK(connect(progress_screen, SIGNAL(ReportWrittenToDesktop(QString)),
                thanks_screen, SLOT(ReportSaved(QString))));
  CHECK(connect(progress_screen, SIGNAL(TestsComplete(ProbeSet::Storage)),
                thanks_screen, SLOT(DisplayFaults(ProbeSet::Storage))));

  CHECK(connect(config_fix_screen, SIGNAL(ClientConfigLogs(QString)),
                progress_screen, SLOT(ClientConfigLogs(QString))));
  CHECK(connect(config_screen, SIGNAL(ConfigChosen(VerificationResult*)),
                config_fix_screen, SLOT(OnConfigChosen(VerificationResult*))));
}

void Wizard::SetupWizard() {
  setOption(ExtendedWatermarkPixmap, false);
  setOption(NoBackButtonOnLastPage, true);
  setOption(NoBackButtonOnStartPage, true);
  setOption(NoCancelButton, false);
  setOption(HaveHelpButton, false);

#ifdef STANDARD_LOGO
  // need to special case the logo for googlemail countries.
  QString logo_path = STANDARD_LOGO;
#ifdef ALTERNATE_LOGO
  if (BrandIsGooglemailHere()) {
    logo_path = ALTERNATE_LOGO;
  }
#endif  // ALTERNATE_LOGO
  QImage logo(logo_path);
#endif  // STANDARD_LOGO

#ifdef COMPANY_NAME
  // provide this much whitespace around the logo
  static const int kTopStandoffX = 15;
  static const int kTopStandoffY = 15;

  static const int kWizardPanelHeight = 350;

  QImage watermark(QSize(logo.width() + 2 * kTopStandoffX,
                         kWizardPanelHeight),
                   QImage::Format_RGB32);
  QPainter painter(&watermark);

  painter.fillRect(watermark.rect(), QBrush(Qt::white));
  QPoint logo_top_left = QPoint(kTopStandoffX, kTopStandoffY);
  QPoint logo_bottom_right = logo_top_left +
                             QPoint(logo.width(), logo.height());
  painter.drawImage(QRect(logo_top_left, logo_bottom_right), logo);

  // the logo and the text don't quite line up right.  Fudge it:
  int manual_x_offset = 10;
  QPoint powered_by_upper_left = QPoint(kTopStandoffX + manual_x_offset,
                                        2 * kTopStandoffY + logo.height());
  QFont font = painter.font();
  font.setFamily("Arial");
  painter.setFont(font);
  painter.setPen(QPen(Qt::darkGray));
  painter.drawText(powered_by_upper_left,
                   tr("powered by %1",
                      "a byline for our logo, %1 is the company name")
                     .arg(COMPANY_NAME));

  setPixmap(QWizard::LogoPixmap, QPixmap(":/graphics/resources/bitmap1.bmp"));
  setPixmap(QWizard::WatermarkPixmap, QPixmap::fromImage(watermark));

  QPalette p = palette();
  p.setBrush(backgroundRole(), QBrush(QColor(Qt::white)));
  setPalette(p);
  setWindowTitle(tr("%1 POP and IMAP Troubleshooter",
                    "The product name, %1 is the company name")
                   .arg(COMPANY_NAME));
#else
  setWindowTitle(tr("POP and IMAP Troubleshooter",
                    "The product name without a company name"));
#endif
}

QSize Wizard::sizeHint() const {
  // If we do not have predefined servers, the additional input fields
  // require more vertical space than the wizard can foresee from the
  // layout above. Unfortunately, neither minimumSizeHint() nor
  // setMinimumHeight() appear to work in this context with Qt version
  // 4.3.3, so we have to specify all dimensions explicitly.
#ifndef __APPLE__
  if (provider->PredefinedServers())
    return QSize(500, 400);
  else
    return QSize(500, 450);
#else
  if (provider->PredefinedServers())
    return QSize(600, 550);
  else
    return QSize(600, 650);
#endif
}

}  // namespace popdiag
